﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LedController
{
    public partial class LedPanel : FlowLayoutPanel
    {
        public Dictionary<string, LedBulb> Leds { get; private set; }

        private Dictionary<string, LedArray> ledArrays = new Dictionary<string, LedArray>();

        private int _elements;
        public int Elements
        {
            get { return _elements; }
            set
            {
                _elements = value;
                foreach (var ledArray in ledArrays.Values)
                {
                    ledArray.Elements = _elements;
                }
                Init();
            }
        }

        private int _ledSize;
        public int LedSize
        {
            get { return _ledSize; }
            set
            {
                _ledSize = value;
                foreach (var ledArray in ledArrays.Values)
                {
                    ledArray.LedSize = _ledSize;
                }
            }
        }

        private int _ledPadding;
        public int LedPadding
        {
            get { return _ledPadding; }
            set
            {
                _ledPadding = value;
                foreach (var ledArray in ledArrays.Values)
                {
                    ledArray.LedPadding = _ledPadding;
                }
            }
        }

        public LedPanel()
        {
            InitializeComponent();
            Leds = new Dictionary<string, LedBulb>();
            LedSize = 120;
            LedPadding = 10;
            Elements = 7;
        }

        public LedPanel(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
            Leds = new Dictionary<string, LedBulb>();
            LedSize = 120;
            LedPadding = 10;
            Elements = 7;
        }


        private void Init()
        {
            //this.SuspendLayout();

            this.Controls.Clear();
            ledArrays.Clear();
            Leds.Clear();

            CreatePort("A", Color.LimeGreen);
            CreatePort("B", Color.Red);
            CreatePort("C", Color.Yellow);
            CreatePort("D", Color.DarkOrange);

            foreach (var array in ledArrays.Values)
                foreach (var led in array.Leds)
                    Leds.Add(led.Key, led.Value); 



            this.FlowDirection = FlowDirection.TopDown;
            this.BackColor = Color.Transparent;
            this.AutoSize = true;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.WrapContents = false;
            //this.PerformLayout();

        }

        private void CreatePort(string Name, Color color)
        {
            var port = new LedArray(Name, this.components);
            port.LedColour = color;
            //port.ArrayName = Name;
            port.Elements = Elements;
            port.LedSize = LedSize;
            ledArrays.Add(port.ArrayName, port);
            this.Controls.Add(port);
        }


    }
}
